package cn.flying.system.web.cm;


import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.base.common.constants.Constants;
import cn.flying.base.common.enums.RtCodeEnum;
import cn.flying.base.common.vo.Rt;
import cn.flying.base.common.vo.TreeTableInfoVo;
import cn.flying.base.common.vo.XmSelect;
import cn.flying.base.core.common.utils.Ft;
import cn.flying.system.common.vo.MenuInfo;
import cn.flying.system.common.vo.ResTreeVo;
import cn.flying.system.common.vo.ResourceUrlVo;
import cn.flying.system.domain.cm.CmResource;
import cn.flying.system.domain.cm.CmResourceUrl;
import cn.flying.system.service.cm.CmCodeService;
import cn.flying.system.service.cm.CmResourceService;

/**
 * @description: 权限资源
 * @author: lvyong
 * @date: 2022年05月22日 17:58
 * @version: 1.0
 */
@Controller
@RequestMapping("resource")
public class CmResourceController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private CmResourceService cmResourceService;
    @Resource
    private CmCodeService cmCodeService;

    /**
     * 获取权限菜单列表-首页左侧导航菜单
     *
     * @return
     */
    @RequestMapping("getResources")
    @ResponseBody
    public Rt getResources() {
        String username = (String) Ft.ThreadLocal.get(Constants.USER_NAME);
        List<MenuInfo> menus = cmResourceService.getPermsResource(username);
        return Rt.result(RtCodeEnum.R_SUCCESS, menus);
    }

    /**
     * 加载菜单树（角色关联资源）
     *
     * @return
     */
    @RequestMapping("getResourceTree")
    @ResponseBody
    public Rt<List<ResTreeVo>> getResourceTree() {
        List<ResTreeVo> permissionTrees = cmResourceService.getResourceTree();
        return Rt.success(permissionTrees);
    }

    /**
     * 列表页面
     *
     * @return
     */
    @RequestMapping("list")
    public String list() {
        return "sys/resource/list";
    }

    /**
     * 加载资源treeTable
     *
     * @return
     */
    @RequestMapping(value = "getResourceTreeTableList", method = RequestMethod.POST)
    @ResponseBody
    public TreeTableInfoVo getResourceTreeTableList() {
        List<CmResource> list = cmResourceService.getResourceTreeTableList();
        return new TreeTableInfoVo(0, "成功", list.size(), list);
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/turn/edit")
    public String turnEdit(Long id, String resId, Model model) {
        Map<String, String> resTypes = cmCodeService.getCodeList("resType");
        model.addAttribute("resTypes", resTypes);
        if (id != null) {
            CmResource resource = cmResourceService.getResourceById(id);
            model.addAttribute("resource", resource);
        } else {
            CmResource resource = new CmResource();
            resource.setParentId(resId);
            model.addAttribute("resource", resource);
        }
        return "sys/resource/edit";
    }

    /**
     * 加载资源url列表数据
     *
     * @return
     */
    @RequestMapping(value = "getResourcePageUrlList", method = RequestMethod.POST)
    @ResponseBody
    public Rt getResourcePageUrlList(String resId) {
        List<CmResourceUrl> list = cmResourceService.getResourcePageUrlList(resId);
        return Rt.success(list);
    }

    /**
     * 保存
     *
     * @param resource
     * @return
     */
    @PostMapping(value = "save")
    @ResponseBody
    public Rt save(@RequestBody ResourceUrlVo resource) {
        if (resource.getId() == null) {
            cmResourceService.insertResource(resource);
        } else {
            cmResourceService.updateResource(resource);
        }

        return Rt.success("保存成功！");
    }

    /**
     * 删除
     *
     * @param resource
     * @return
     */
    @PostMapping(value = "del")
    @ResponseBody
    public Rt del(@RequestBody CmResource resource) {
        cmResourceService.deleteResourceById(resource.getId());
        return Rt.success("删除成功！");
    }

    /**
     * xm-select 资源下拉框数据
     *
     * @return
     */
    @PostMapping(value = "getResourceSelectList")
    @ResponseBody
    public Rt<List<XmSelect>> getResourceSelectList() {
        List<XmSelect> treeList = cmResourceService.getResourceSelectList();
        return Rt.success(treeList);
    }
}
